/**
 * 
 */
package info.niwota.start;

import info.niwota.start.app.AppLoader;

import java.util.List;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.RemoteViews;

/**
 * @author qiangli
 *
 */
public class Notifications {
	
	private static final int NOTIFY_ID = R.string.app_name;

	private static final String TAG = "Notifications";

//	private static StringBuilder sFormatBuilder = new StringBuilder();
//	private static Formatter sFormatter = new Formatter(sFormatBuilder, Locale
//			.getDefault());
//
//	private static String makeLabel(Context ctx, int n) {
//		StringBuilder sb = new StringBuilder();
//		Resources r = ctx.getResources();
//		//
//		String f = r.getQuantityText(R.plurals.N_applications, n).toString();
//		sFormatBuilder.setLength(0);
//		sFormatter.format(f, Integer.valueOf(n));
//		sb.append(sFormatBuilder);
//		sb.append(ctx.getString(R.string.separator));
//		return sb.toString();
//	}

//	private static void removeStatusbar(Context ctx) {
//		List<ResourceInfo> list = AppLoader.loadStatusbar(ctx);
//		int c = (list == null ? 0 : list.size());
//		NotificationManager nm = (NotificationManager) ctx
//				.getSystemService(Context.NOTIFICATION_SERVICE);
//		for (int i = 0; i < c; i++) {
//			try {
//				ResourceInfo ri = list.get(i);
//				int nid = makeNoteId(ri.getRowId());
//				nm.cancel(nid);
//			} catch (Exception e) {
//				if (Debug.DEBUG) {
//					e.printStackTrace();
//				}
//			}
//		}
//	}

	private static void addStatusbar(Context ctx) {	
		List<ResourceInfo> list = AppLoader.loadStatusbar(ctx);
		int c = (list == null ? 0 : list.size());
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(Context.NOTIFICATION_SERVICE);
		//
		for (int i = 0; i < c; i++) {
			try {
				ResourceInfo ri = list.get(i);
				Intent in = ri.getIntent();
				if (Debug.DEBUG) {
					Log.d(TAG, "add to status bar: " + ri);
				}
				int icon = R.drawable.icon_dot; //invisible
				Notification note = new Notification(icon, null, 0);
				PendingIntent pi = PendingIntent.getActivity(ctx, 0, in, 0);
				note.flags |= Notification.FLAG_NO_CLEAR;
				note.flags |= Notification.FLAG_ONGOING_EVENT;
				//
				CharSequence title = ri.getTitle();
				//note.setLatestEventInfo(ctx, title, null, pi);
				Drawable d = ri.getIcon();
				RemoteViews contentView = new RemoteViews(ctx.getPackageName(), R.layout.notification);
				if (d instanceof BitmapDrawable) {
					BitmapDrawable bd = (BitmapDrawable) d;
					Bitmap bitmap = bd.getBitmap();
					contentView.setImageViewBitmap(R.id.icon, bitmap);
				}
				contentView.setTextViewText(R.id.title, title);
				
				note.contentView = contentView;
				note.contentIntent = pi;
				int nid = makeNoteId(i);
				nm.notify(nid, note);
			} catch (Exception e) {
				if (Debug.DEBUG) {
					e.printStackTrace();
				}
			}
		}
	}

	private static int makeNoteId(int n) {
		int nid = NOTIFY_ID + 1 + Math.abs( n); 
		return nid;
	}
	
	public static void update(Context ctx) {
		//call add
		add(ctx);
	}

	public static void add(Context ctx) {
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(Context.NOTIFICATION_SERVICE);
		//
		Notification note = new Notification(R.drawable.icon25, null, 0);
		PendingIntent pi = PendingIntent.getActivity(ctx, 0, new Intent(ctx,
				StartActivity.class), 0);
		note.flags |= Notification.FLAG_NO_CLEAR;
		note.flags |= Notification.FLAG_ONGOING_EVENT;
		//
		//CharSequence title = ctx.getText(R.string.app_tagline);
		//CharSequence desc = ctx.getText(R.string.app_desc);
		//note.setLatestEventInfo(ctx, title, desc, pi);
		RemoteViews contentView = new RemoteViews(ctx.getPackageName(), R.layout.start_notification);
		note.contentView = contentView;
		note.contentIntent = pi;
		//
		//refresh by cancelling all first
		nm.cancelAll();
		//
		addStatusbar(ctx);
		
		//add start last so it will always show its icon on status bar

		nm.notify(NOTIFY_ID, note);
	}

	public static void remove(Context ctx) {
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(Context.NOTIFICATION_SERVICE);
		nm.cancelAll();
	}

	private Notifications() {}

}
